﻿using mozhi.smarterp.Finance.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace mozhi.smarterp.Finance.Finances
{
    /// <summary>
    /// 凭证模板，用于业务单据生成凭证
    /// </summary>
    public class JournalTemplate : AggregateRootBase<Guid>
    {
        public DocumentType DocumentType { get; set; }

        public string Comment { private set; get; }

        /// <summary>
        /// 凭证字
        /// </summary>
        public Guid NumberPrefixId { private set; get; }

        public JournalItemMergerRuleType MergerRuleType { get; set; }

        public List<JournalTemplateItem> Items { private set; get; }

        private JournalTemplate() { }

        public JournalTemplate(Guid id, DocumentType type , string comment , Guid numberPrefixId)
        {
            Id = id;
            DocumentType = type;
            Comment = comment;
            NumberPrefixId = numberPrefixId;

            Items = new List<JournalTemplateItem>();

            Validate();
        }

        public void ChangeItems(List<JournalTemplateItem> items)
        {
            foreach (var item in items)
            {
                var match = Items.FirstOrDefault(x => x.Id == item.Id);

                if (match != null)
                    match.Change(item.AccountId, item.JournalDirection, item.Index);
                else
                    Items.Add(item);
            }

            var itemIds = items.Select(x => x.Id).ToList();

            var removedItems = Items.Where(x => !itemIds.Contains(x.Id)).ToList();

            foreach (var removedItem in removedItems)
            {
                Items.Remove(removedItem);
            }

        }

        #region
        private void Validate()
        {
            if(NumberPrefixId==default)
                throw new UserFriendlyException("凭证字不能为空", "100000");
        }
        #endregion

    }


    public class JournalTemplateItem : EntityBase<Guid>
    {
        public List<JournalItemGenerationSummaryRule> GenerationSummaryRules { private set; get; }

        public Guid AccountId { private set; get; }

        public AccountDirectionType JournalDirection { private set; get; }

        public int Index { set; get; }

        private JournalTemplateItem() { }

        public JournalTemplateItem(Guid id,  Guid accountId, AccountDirectionType direction,  int index)
        {
            this.Id = id;
            this.AccountId = accountId;
            this.JournalDirection = direction;


            this.Index = index;

            Validate();
        }



        public void Change(Guid accountId, AccountDirectionType direction,  int index)
        {
            this.AccountId = accountId;
            this.JournalDirection = direction;

            this.Index = index;

            Validate();
        }

        #region
        private void Validate()
        {
           
            if (AccountId == default)
                throw new UserFriendlyException("科目不能为空", "100000");

            if (JournalDirection == default)
                throw new UserFriendlyException("凭证方向不能为空", "100000");

           
            if (Index == 0)
                throw new UserFriendlyException("分录序号不能为空", "100000");
        }
        #endregion

    }

    public class JournalItemGenerationSummaryRule : Entity<Guid>
    {
         
    }
}
